<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>vue项目中的痛点（十四） | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Vue,">
  

  <meta name="description" content="一、列表进入详情页传参 例如商品列表页面前往商品详情页面，需要传一个商品id  &amp;lt;router-link :to=&quot;&amp;#123;path: &apos;detail&apos;, query: &amp;#123;id: 1&amp;#125;&amp;#125;&quot;&amp;gt;前往detail页面&amp;lt;/router-link&amp;gt;  c页面的路径为http://localhost:8080/#/detail?id=1，可以看到传了一">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue项目中的痛点（十四）">
<meta property="og:url" content="http://blog.poetries.top/2018/08/28/vue-project-dev-question/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、列表进入详情页传参 例如商品列表页面前往商品详情页面，需要传一个商品id  &amp;lt;router-link :to=&quot;&amp;#123;path: &apos;detail&apos;, query: &amp;#123;id: 1&amp;#125;&amp;#125;&quot;&amp;gt;前往detail页面&amp;lt;/router-link&amp;gt;  c页面的路径为http://localhost:8080/#/detail?id=1，可以看到传了一">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue项目中的痛点（十四）">
<meta name="twitter:description" content="一、列表进入详情页传参 例如商品列表页面前往商品详情页面，需要传一个商品id  &amp;lt;router-link :to=&quot;&amp;#123;path: &apos;detail&apos;, query: &amp;#123;id: 1&amp;#125;&amp;#125;&quot;&amp;gt;前往detail页面&amp;lt;/router-link&amp;gt;  c页面的路径为http://localhost:8080/#/detail?id=1，可以看到传了一">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、列表进入详情页传参"><span class="toc-text">一、列表进入详情页传参</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、请求服务器接口跨域"><span class="toc-text">二、请求服务器接口跨域</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、axios的封装和API接口的统一管理"><span class="toc-text">三、axios的封装和API接口的统一管理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、UI库的按需加载"><span class="toc-text">四、UI库的按需加载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、定时器问题"><span class="toc-text">五、定时器问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、rem文件的导入问题"><span class="toc-text">六、rem文件的导入问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#七、打包后生成很大的-map文件"><span class="toc-text">七、打包后生成很大的.map文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#八、fastClick的300ms延迟"><span class="toc-text">八、fastClick的300ms延迟</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#九、路由懒加载（延迟加载）"><span class="toc-text">九、路由懒加载（延迟加载）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十、开启gzip压缩代码"><span class="toc-text">十、开启gzip压缩代码</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-vue-project-dev-question" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">vue项目中的痛点（十四）</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.08.28</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 2k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 8分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、列表进入详情页传参"><a href="#一、列表进入详情页传参" class="headerlink" title="一、列表进入详情页传参"></a>一、列表进入详情页传参</h2><blockquote>
<p>例如商品列表页面前往商品详情页面，需要传一个商品id</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">router-link</span> <span class="attr">:to</span>=<span class="string">"&#123;path: 'detail', query: &#123;id: 1&#125;&#125;"</span>&gt;</span>前往detail页面<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>c</code>页面的路径为<code>http://localhost:8080/#/detail?id=1</code>，可以看到传了一个参数<code>id=1</code>，并且就算刷新页面id也还会存在。此时在c页面可以通过id来获取对应的详情数据，获取<code>id</code>的方式是<code>this.$route.query.id</code></p>
</blockquote>
<p><strong>vue传参方式有：query、params+动态路由传参</strong></p>
<blockquote>
<p><code>query</code>通过<code>path</code>切换路由，<code>params</code>通过<code>name</code>切换路由</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">// query通过path切换路由</span><br><span class="line"><span class="tag">&lt;<span class="name">router-link</span> <span class="attr">:to</span>=<span class="string">"&#123;path: 'Detail', query: &#123; id: 1 &#125;&#125;"</span>&gt;</span>前往Detail页面<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br><span class="line">// params通过name切换路由</span><br><span class="line"><span class="tag">&lt;<span class="name">router-link</span> <span class="attr">:to</span>=<span class="string">"&#123;name: 'Detail', params: &#123; id: 1 &#125;&#125;"</span>&gt;</span>前往Detail页面<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>query</code>通过<code>this.$route.query</code>来接收参数，<code>params</code>通过<code>this.$route.params</code>来接收参数</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// query通过this.$route.query接收参数</span></span><br><span class="line">created () &#123;</span><br><span class="line">    <span class="keyword">const</span> id = <span class="keyword">this</span>.$route.query.id;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// params通过this.$route.params来接收参数</span></span><br><span class="line">created () &#123;</span><br><span class="line">    <span class="keyword">const</span> id = <span class="keyword">this</span>.$route.params.id;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><code>query</code>传参的<code>url</code>展现方式：<code>/detail?id=1&amp;user=123&amp;identity=1&amp;</code>更多参数</li>
<li><code>params</code>＋动态路由的<code>url</code>方式：<code>/detail/123</code></li>
<li><code>params</code>动态路由传参，一定要在路由中定义参数，然后在路由跳转的时候必须要加上参数，否则就是空白页面</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#123;      </span><br><span class="line">    path: &apos;/detail/:id&apos;,      </span><br><span class="line">    name: &apos;Detail&apos;,      </span><br><span class="line">    component: Detail    </span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>注意</strong>，<code>params</code>传参时，如果没有在路由中定义参数，也是可以传过去的，同时也能接收到，但是一旦刷新页面，这个参数就不存在了。这对于需要依赖参数进行某些操作的行为是行不通的</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">// 定义的路由中，只定义一个id参数</span><br><span class="line">&#123;</span><br><span class="line">    path: 'detail/:id',</span><br><span class="line">    name: 'Detail',</span><br><span class="line">    components: Detail</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// template中的路由传参，</span><br><span class="line">// 传了一个id参数和一个token参数</span><br><span class="line">// id是在路由中已经定义的参数，而token没有定义</span><br><span class="line"><span class="tag">&lt;<span class="name">router-link</span> <span class="attr">:to</span>=<span class="string">"&#123;name: 'Detail', params: &#123; id: 1, token: '123456' &#125;&#125;"</span>&gt;</span>前往Detail页面<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br><span class="line"></span><br><span class="line">// 在详情页接收</span><br><span class="line">created () &#123;</span><br><span class="line">    // 以下都可以正常获取到</span><br><span class="line">    // 但是页面刷新后，id依然可以获取，而token此时就不存在了</span><br><span class="line">    const id = this.$route.params.id;</span><br><span class="line">    const token = this.$route.params.token;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>综上：尽量使用<code>query</code>来传参</p>
</blockquote>
<h2 id="二、请求服务器接口跨域"><a href="#二、请求服务器接口跨域" class="headerlink" title="二、请求服务器接口跨域"></a>二、请求服务器接口跨域</h2><blockquote>
<p>本地开发项目请求服务器接口的时候，因为客户端的同源策略，导致了跨域的问题</p>
</blockquote>
<ul>
<li><code>vue-cli</code>初始化的项目，在配置文件中提供了<code>proxyTable</code>来解决本地开发的跨域问题。<code>config</code>文件的<code>index.js</code>文件中，找到<code>proxyTable</code>选项，进行如下配置</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">proxyTable: &#123;</span><br><span class="line">      <span class="comment">// 用‘/api’开头，代理所有请求到目标服务器</span></span><br><span class="line">      <span class="string">'/api'</span>: &#123;</span><br><span class="line">        target: <span class="string">'http://jsonplaceholder.typicode.com'</span>, <span class="comment">// 接口域名</span></span><br><span class="line">        changeOrigin: <span class="literal">true</span>, <span class="comment">// 是否启用跨域</span></span><br><span class="line">        pathRewrite: &#123; <span class="comment">//</span></span><br><span class="line">          <span class="string">'^/api'</span>: <span class="string">''</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>例如请求接口：<code>/api/posts/1 ==&gt;http://jsonplaceholder.typicode.com/posts/1</code></li>
<li>这个时候就可以在本地环境请求后台接口了</li>
</ul>
<h2 id="三、axios的封装和API接口的统一管理"><a href="#三、axios的封装和API接口的统一管理" class="headerlink" title="三、axios的封装和API接口的统一管理"></a>三、axios的封装和API接口的统一管理</h2><ul>
<li><code>axios</code>的封装，主要是用来帮我们进行请求的拦截和响应的拦截。</li>
<li>在请求的拦截中我们可以携带<code>userToken</code>，<code>post</code>请求头、<code>qs</code>对<code>post</code>提交数据的序列化等- 在响应的拦截中，我们可以进行根据状态码来进行错误的统一处理等等。</li>
<li><code>axios</code>接口的统一管理，是做项目时必须的流程。这样可以方便我们管理我们的接口，在接口更新时我们不必再返回到我们的业务代码中去修改接口</li>
</ul>
<h2 id="四、UI库的按需加载"><a href="#四、UI库的按需加载" class="headerlink" title="四、UI库的按需加载"></a>四、UI库的按需加载</h2><blockquote>
<p>这里以vant的按需加载为例，演示vue中ui库怎样进行按需加载</p>
</blockquote>
<ul>
<li>安装： <code>cnpm i vant -S</code></li>
<li>安装<code>babel-plugin-import</code>插件使其按需加载：  <code>cnpm i babel-plugin-import -D</code></li>
<li>在 <code>.babelrc</code>文件中中添加插件配置 </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">libraryDirectory &#123; </span><br><span class="line"></span><br><span class="line">    <span class="string">"plugins"</span>: [ </span><br><span class="line">        <span class="comment">// 这里是原来的代码部分</span></span><br><span class="line">        <span class="comment">// …………</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 这里是要我们配置的代码</span></span><br><span class="line">        [<span class="string">"import"</span>, </span><br><span class="line">            &#123; </span><br><span class="line">                <span class="string">"libraryName"</span>: <span class="string">"vant"</span>, </span><br><span class="line">                <span class="string">"libraryDirectory"</span>: <span class="string">"es"</span>, </span><br><span class="line">                <span class="string">"style"</span>: <span class="literal">true</span> </span><br><span class="line">            &#125;</span><br><span class="line">        ] </span><br><span class="line">    ] </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在<code>main.js</code>中按需加载你需要的插件</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 按需引入vant组件</span></span><br><span class="line"><span class="keyword">import</span> &#123;   </span><br><span class="line">    DatetimePicker,   </span><br><span class="line">    Button,   </span><br><span class="line">    List </span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">'vant'</span>;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用组件</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 使用vant组件</span></span><br><span class="line">Vue.use(DatetimePicker)  </span><br><span class="line">    .use(Button)  </span><br><span class="line">    .use(List);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>最后在在页面中使用：</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">van-button</span> <span class="attr">type</span>=<span class="string">"primary"</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">van-button</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="五、定时器问题"><a href="#五、定时器问题" class="headerlink" title="五、定时器问题"></a>五、定时器问题</h2><blockquote>
<p>在a页面写一个定时，让他每秒钟打印一个1，然后跳转到b页面，此时可以看到，定时器依然在执行。这样是非常消耗性能的</p>
</blockquote>
<p><strong>解决方案一</strong></p>
<blockquote>
<p>在data函数里面进行定义定时器名称</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">data() &#123;            </span><br><span class="line">    return &#123;                              </span><br><span class="line">        timer: null  // 定时器名称          </span><br><span class="line">    &#125;        </span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<blockquote>
<p>然后这样使用定时器</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">this.timer = (() =&gt; &#123;</span><br><span class="line">    // 某些操作</span><br><span class="line">&#125;, 1000)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>最后在<code>beforeDestroy()</code>生命周期内清除定时器</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">beforeDestroy() &#123;</span><br><span class="line">    clearInterval(<span class="keyword">this</span>.timer);        </span><br><span class="line">    <span class="keyword">this</span>.timer = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>方案1有两点不好的地方</p>
</blockquote>
<ul>
<li>它需要在这个组件实例中保存这个 timer，如果可以的话最好只有生命周期钩子可以访问到它。这并不算严重的问题，但是它可以被视为杂物。</li>
<li>我们的建立代码独立于我们的清理代码，这使得我们比较难于程序化的清理我们建立的所有东西</li>
</ul>
<p><strong>解决方案2</strong></p>
<blockquote>
<p>该方法是通过<code>$once</code>这个事件侦听器器在定义完定时器之后的位置来清除定时器。以下是完整代码</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> timer = setInterval(<span class="function"><span class="params">()</span> =&gt;</span>&#123;                    </span><br><span class="line">    <span class="comment">// 某些定时器操作                </span></span><br><span class="line">&#125;, <span class="number">500</span>);            </span><br><span class="line"><span class="comment">// 通过$once来监听定时器，在beforeDestroy钩子可以被清除。</span></span><br><span class="line"><span class="keyword">this</span>.$once(<span class="string">'hook:beforeDestroy'</span>, () =&gt; &#123;            </span><br><span class="line">    clearInterval(timer);                                    </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="六、rem文件的导入问题"><a href="#六、rem文件的导入问题" class="headerlink" title="六、rem文件的导入问题"></a>六、rem文件的导入问题</h2><blockquote>
<p>在做手机端时，适配是必须要处理的一个问题。例如，我们处理适配的方案就是通过写一个<code>rem.js</code>，原理很简单，就是根据网页尺寸计算<code>html</code>的<code>font-size</code>大小</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">; (<span class="function"><span class="keyword">function</span>(<span class="params">c, d</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> e = <span class="built_in">document</span>.documentElement || <span class="built_in">document</span>.body,</span><br><span class="line">    a = <span class="string">"orientationchange"</span> <span class="keyword">in</span> <span class="built_in">window</span> ? <span class="string">"orientationchange"</span>: <span class="string">"resize"</span>,</span><br><span class="line">    b = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> f = e.clientWidth;</span><br><span class="line">        e.style.fontSize = (f &gt;= <span class="number">750</span>) ? <span class="string">"100px"</span>: <span class="number">100</span> * (f / <span class="number">750</span>) + <span class="string">"px"</span></span><br><span class="line">    &#125;;</span><br><span class="line">    b();</span><br><span class="line">    c.addEventListener(a, b, <span class="literal">false</span>)</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在<code>main.js</code>中，直接<code>import &#39;./config/rem&#39;</code>导入即可。<code>import</code>的路径根据你的文件路径去填写</p>
</blockquote>
<h2 id="七、打包后生成很大的-map文件"><a href="#七、打包后生成很大的-map文件" class="headerlink" title="七、打包后生成很大的.map文件"></a>七、打包后生成很大的.map文件</h2><blockquote>
<p>项目打包后，代码都是经过压缩加密的，如果运行时报错，输出的错误信息无法准确得知是哪里的代码报错。 而生成的<code>.map</code>后缀的文件，就可以像未加密的代码一样，准确的输出是哪一行哪一列有错可以通过设置来不生成该类文件。但是我们在生成环境是不需要<code>.map</code>文件的，所以可以在打包时不生成这些文件</p>
</blockquote>
<ul>
<li>在<code>config/index.js</code>文件中，设置<code>productionSourceMap: false</code>,就可以不生成<code>.map</code>文件</li>
</ul>
<h2 id="八、fastClick的300ms延迟"><a href="#八、fastClick的300ms延迟" class="headerlink" title="八、fastClick的300ms延迟"></a>八、fastClick的300ms延迟</h2><blockquote>
<p>在<code>main.js</code>中引入<code>fastClick</code>和初始化</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> FastClick <span class="keyword">from</span> <span class="string">'fastclick'</span>; <span class="comment">// 引入插件</span></span><br><span class="line">FastClick.attach(<span class="built_in">document</span>.body); <span class="comment">// 使用 fastclick</span></span><br></pre></td></tr></table></figure>
<h2 id="九、路由懒加载（延迟加载）"><a href="#九、路由懒加载（延迟加载）" class="headerlink" title="九、路由懒加载（延迟加载）"></a>九、路由懒加载（延迟加载）</h2><blockquote>
<p>路由懒加载可以帮我们在进入首屏时不用加载过度的资源，从而减少首屏加载速度</p>
</blockquote>
<p><strong>非懒加载写法</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Index <span class="keyword">from</span> <span class="string">'@/page/index/index'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;  </span><br><span class="line">    routes: [    </span><br><span class="line">        &#123; </span><br><span class="line">            path: <span class="string">'/'</span>, </span><br><span class="line">            name: <span class="string">'Index'</span>,     </span><br><span class="line">            component: Index </span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p><strong>路由懒加载写法</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</span><br><span class="line">  routes: [    </span><br><span class="line">        &#123; </span><br><span class="line">            path: <span class="string">'/'</span>, </span><br><span class="line">            name: <span class="string">'Index'</span>, </span><br><span class="line">            component: <span class="function"><span class="params">resolve</span> =&gt;</span> <span class="built_in">require</span>([<span class="string">'@/view/index/index'</span>], resolve) </span><br><span class="line">        &#125;</span><br><span class="line">   ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="十、开启gzip压缩代码"><a href="#十、开启gzip压缩代码" class="headerlink" title="十、开启gzip压缩代码"></a>十、开启gzip压缩代码</h2><blockquote>
<p><code>spa</code>这种单页应用，首屏由于一次性加载所有资源，所有首屏加载速度很慢。解决这个问题非常有效的手段之一就是前后端开启<code>gizp</code>（其他还有缓存、路由懒加载等等）。<code>gizp</code>其实就是帮我们减少文件体积，能压缩到<code>30%</code>左右，即<code>100k</code>的文件<code>gizp</code>后大约只有<code>30</code>k`</p>
</blockquote>
<ul>
<li><code>vue-cli</code>初始化的项目中，是默认有此配置的，只需要开启即可。但是需要先安装插件</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">npm i compression-webpack-plugin</span><br></pre></td></tr></table></figure>
<ul>
<li>在<code>config/index.js</code>中开启即可</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">build: &#123;</span><br><span class="line">    ………………</span><br><span class="line">    productionGzip: true, // false不开启gizp，true开启</span><br><span class="line">    ………………</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里前端进行的打包时的<code>gzip</code>，但是还需要后台服务器的配置。配置是比较简单的，配置几行代码就可以了</p>
</blockquote>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/08/28/vue-$vm-method-props/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/08/28/vue-animation-transition/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
